<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-8
 * Time: 上午11:14
 */

/**
$exportImpl = new ExportBaseImpl();
$exportImpl->setMapping(array(
    'id' => array(
        'title' => 'ID',
    )
));
$exportImpl->setData($data);
$exportImpl->render();


 */
namespace impls;
class ExportBaseImpl extends BaseImpl {
    protected $mapping = array();
    protected $data = array();
    protected $fileName = '';

    //导出类型
    const EXPORT_TYPE_TXT = 'txt';

    public function setMapping($mapping = array()) {
        $defaultMapping = array(
            'id' => array(
                'title' => 'ID',
            ),
            'add_time' => array(
                'title' => '添加时间',
                'format' => array('type' => '__date', 'param' => 'Y-m-d H:i:s')
            ),
            'status' => array(
                'title' => '状态',
                'format' => array('type' => '__map', 'param' => array('0' => '等待支付', 1 => '支付成功', 2 => '订单完成'))
            ),
        );
        $this->mapping = $mapping;
    }

    public function getMapping() {
        return $this->mapping;
    }

    public function setData($data = array()) {
        $this->data = $data;
    }

    public function getData() {
        return $this->data;
    }

    //导出的文件名称
    public function setFileName($fileName) {
        $this->fileName = $fileName;
    }

    public function getFileName() {
        return $this->fileName;
    }

    public function render($exportType = 'txt') {
        if($exportType == self::EXPORT_TYPE_TXT) {
            $this->renderTxt();
        }
    }

    protected function renderTxt() {
        $data = $this->getFormatedData();
        $fileName = $this->getFileName();
        $fileName = $fileName . '_' . date('YmdHis');

        $content = '';
        foreach($data as $row) {
            $content .= implode(',', $row) . "\r\n";
        }

        header("Pragma: public");
        header("Cache-control: max-age=0");
        header('Cache-Control: no-store, no-cache, must-revalidate');
        header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
        header("Content-Disposition: attachment; filename={$fileName}.txt");
        header("Content-Length: ".strlen($content));
        header("Content-type: text/comma-separated-values");
        header('Content-Encoding: none');
        header("Content-Transfer-Encoding: binary" );

        echo $content;
    }

    protected function getFormatedData() {
        $mapping = $this->getMapping();
        $data = $this->getData();
        $titles = array();
        $list = array();
        foreach($data as $i => $row) {
            foreach($row as $key => $value) {
                $map = $mapping[$key];
                if(!$map) continue;

                $titles[$key] = $map['title'];
                $format = $map['format'];
                $list[$i][$key] = $value;
                if($format) {
                    $type = $format['type'];
                    $param = $format['param'];
                    $list[$i][$key] = $this->{$type}($param, $key, $value, $row);
                }
            }
        }
        return array($titles) + $list;
    }

    //自定义格式函数
    protected function __date($param, $key, $value, $row) {
        return date($param, $value);
    }

    //自定义格式函数
    protected function __map($param, $key, $value, $row) {
        return $param[$value];
    }
}